<?
session_start();
function deletec($idp, $idc)
{
$resultz=returnQuery("select * from project_companies where id_project='".$idp."' and id_company='".$idc."';", $dbh);
$rowz=mysql_fetch_assoc($resultz);
$resulty=returnQuery("delete from project_team where id_project_company='".$rowz['id']."';", $dbh);
$resultw=returnQuery("delete from project_companies where id_project='".$idp."' and id_company='".$idc."';", $dbh);
echo "deleted";

	//$result=returnQuery("select * from ");
	

}

function deletep($idp, $idc, $idhr)
{
echo "idp= ".$idp.".idc  ".$idc.".idhr  ".$idhr;
$resultz=returnQuery("select * from project_companies where id_project='".$idp."' and id_company='".$idc."';", $dbh);
	while ($rowz=mysql_fetch_assoc($resultz)) 
		{
			echo "id project company ".$rowz['id'];
			$resulte= returnQuery("delete from project_team where id_project_company='".$rowz['id']."' and id_human_resource='".$idhr."';", $dbh);
			}

}
$account =  $_SESSION["account"] ;
// functie de adaugare persoana
function display_com($idp)
{
	
//	$searchComp = returnQuery("SELECT name, id FROM companies where id in (select id_company from project_companies where id_project=".$idp.");",$dbh);
	$searchEmp = returnQuery("SELECT name, surname, id, id_company FROM human_resources;",$dbh);
	
/*	$i = 0; 
	$arrComps = array(); 
	$arrCompsID = array();
	while ($row = mysql_fetch_array($searchComp)) { 
		$arrComps[$i] = $row["name"]; 
		$arrCompsID[$i] = $row["id"];
		$i++; 
	} */
	$j = 0; 
	$arrEmps = array(); 
	$arrEmpsID = array(); 
	$arrEmpsC = array();
	while ($row = mysql_fetch_array($searchEmp)) { 
		$arrEmps[$j] = $row["name"]." ".$row["surname"]; 
		$arrEmpsID[$j] = $row["id"];
		$arrEmpsC[$j] = $row["id_company"];
		$j++; 
	} 
	
	?> 
	
	<script type="text/javascript"> 
		arrComp = new Array(); 
		arrCompID = new Array();
		<?php 
		for($i=0;$i<count($arrComps); $i++){ 
			echo "arrComp[$i]='".$arrComps[$i]."';\n"; 
			echo "arrCompID[$i]='".$arrCompsID[$i]."';\n";
		} 
		?>  
		arrEmp = new Array(); 
		arrEmpID = new Array(); 
		arrEmpC = new Array();
		<?php 
		for($i=0;$i<count($arrEmps); $i++){ 
			echo "arrEmp[$i]='".$arrEmps[$i]."';\n"; 
			echo "arrEmpID[$i]='".$arrEmpsID[$i]."';\n"; 
			echo "arrEmpC[$i]='".$arrEmpsC[$i]."';\n"; 
		} 
		?> 
		

	function umpleComp(idx)
	{
	
		var f=document.adaugapers;
		f.sel_pers.options.length=null;
	
		if (idx==0) 
		{		
			/*f.pers.options.length=null;
			
			for(var i=0; i<arrComp.length; i++) {
				f.pers.options[i] = new Option(arrComp[i],arrCompID[i]);
			}
			f.pers.options[0].selected = true; */
					
		} 
		else 
		{
		
		var j=0;		
			
			for(var i=0; i<arrEmp.length; i++) 
			{
				if (arrEmpC[i]==idx) 
				{
					f.sel_pers.options[j] = new Option(arrEmp[i],arrEmpID[i]);
					j++;
					
				} 
			}
			f.sel_pers.options[0].selected = true; 
		}
	}
	
	</script>
			
			
	
	<body onLoad="umpleComp(0);">
    <form action="" name="adaugapers" method="post" >
    <table width="90%">
	<tr><td width="90"> Pick company </td> <td>
		<select name="comp" onChange="umpleComp(this.value);" style="width:250px;">
		<?php
				echo "<option>-- Select Item --</option>\n";
				$instructiune="SELECT name, id FROM companies where id in (select id_company from project_companies where id_project=".$idp." );";
				$interogare=mysql_query($instructiune);
				while($rand=mysql_fetch_array($interogare))
					{
						if($_GET["id_company"]==$rand['id']) $selected="selected";
							else $selected="";
						echo("\t   <option {".$selected."} value='".$rand['id']."'> ".$rand['name']." </option>  \n");
						
					}
		?>
		</select> </td> </tr>
	<tr><td width="90"> Pick person </td> <td> <select name="sel_pers" style="width:250px;"> </select> </td></tr>
	<tr>  <td> <input type="submit" name="aplica" value="Apply" align="right"> </td>
	   	<td align="right"><input type="submit" name="qr_cancel" value="Cancel"></td></tr>
	</table>
	</form>
<?
}



 

function show_company_info($idc, $idp)
{
	$result1=mysql_query("select id from project_companies where id_project=".$idp." and id_company=".$idc.";");
	while ($row1=mysql_fetch_array($result1))
	{
		$result2=mysql_query("select * from project_team where id_project_company=".$row1['id'].";");
		 ?> <tr> <td> </td> <td>
    <table   width=100%> <?
	while ($row2=mysql_fetch_array($result2))
		{
		$result3=mysql_query("select * from human_resources where id=".$row2['id_human_resource'].";");
		$row3=mysql_fetch_array($result3);
				
		{
        echo "<tr><tr>
						<th width='50%'>							
							<a href='project_details.php?rowid=".$idp."&amp;rowc=".$idc."&amp;idhr=".$row2['id_human_resource']."&amp;delp=1;' > 
								<img src='images/delete.png' width='20' height='18' /> 
							</a>
						| 	
						";
   		    echo $row2['hr_rank']?></th> <td> <? echo $row3['name'] ?> <? echo $row3['surname'] ?> </td> </tr>
		<?
        }	
}		

        
       ?> </table>
        </td>
        </tr>
        
        <?
		
	} 
	
      
}
    $account = $_SESSION["account"];
//display section:
function display($idp ,$account)
{
	//if($x==1)
	//{
	?> <form action="" name="adaugam" method="post" >
    <table width="90%">
    <tr><td> Company </td> <td>
    	<form  action="project_details.php" method="get">
			<select name="company" onChange="" style="width:250px;">
 			<?php
    		echo "<option>-- Select Item --</option>\n";
			$instructiune="SELECT name FROM companies WHERE id NOT IN ( SELECT id_company FROM project_companies WHERE id_project =".$idp.") and account = '".$_SESSION['account']."';";
			$interogare=mysql_query($instructiune);
			while($rand=mysql_fetch_array($interogare))
				{
					if($_GET["company"]==$rand['name']) $selected="selected";
						else $selected="";
					echo("\t   <option {".$selected."} value='".$rand['name']."'> ".$rand['name']." </option>  \n");
			
				}
			?>
            
			</select>
           <!-- </form> -->
             </td></tr>
	<tr> <td> Company type </td><td>
    	<!--<form  action="project_details.php" method="get"> -->
			<select name="company_type" onChange="" style="width:250px;">
 			<?php
    		echo "<option>-- Select Item --</option>\n";
			$instructiune="SELECT type FROM company_type;";
			$interogare=mysql_query($instructiune);
			while($rand=mysql_fetch_array($interogare))
				{
					if($_GET["company_type"]==$rand['type']) $selected="selected";
						else $selected="";
					echo("\t   <option {".$selected."} value='".$rand['type']."'> ".$rand['type']." </option>  \n");
			
				}
			?>
            
			</select>
            </form> </td></tr>
        <tr> <td>  <input type="submit" name="add_company" value="Add" align="right"> </td>
		<td align="right"><input type="submit" name="qr_cancel" value="Cancel"></td></tr>
        </table>
	</form>
    

	
 <?	
   
}

	


?>